from fastapi import FastAPI
from fastapi import Request
from api.book import api_book
from pydantic import BaseModel
import uvicorn


class body(BaseModel):
    name: str
    age: int

    def addAge(self, age: int):
        self.age = self.age + age


webapp = FastAPI()

webapp.include_router(api_book, prefix="/api", tags=["书本api"])


@webapp.get("/hello")
async def getHello():
    return "Hello World3"


@webapp.get("/json")
async def getJson(req: Request):
    print(req.query_params)
    return {"hello": 4}


@webapp.post("/post_json")
async def getJson(req: Request, body: body):
    # body = await req.json()
    print(body)
    body.addAge(10)
    body.add(body)
    print(body)
    return {"hello": 1}


if __name__ == '__main__':
    uvicorn.run("web:webapp", host="127.0.0.1", port=8000, reload=True)
    # uvicorn.run(webapp, host="127.0.0.1", port=8000)
